package com.zggk.iroad.main;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zggk.iroad.api.MyAppliCation;
import com.zggk.iroad.api.Urls;
import com.zggk.iroad.callback.StringDialogCallback;
import com.zggk.iroad.db.BhInfo;
import com.zggk.iroad.db.BhLxInfo;
import com.zggk.iroad.db.DcLxInfo;
import com.zggk.iroad.db.EntityManager;
import com.zggk.iroad.db.LxInfo;
import com.zggk.iroad.db.XcXxInfo;
import com.zggk.iroad.greendao.BhInfoDao;
import com.zggk.iroad.greendao.BhLxInfoDao;
import com.zggk.iroad.greendao.DcLxInfoDao;
import com.zggk.iroad.greendao.LxInfoDao;
import com.zggk.iroad.greendao.XcXxInfoDao;
import com.zggk.iroad.mvp.BasePresenterImpl;

import java.util.List;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MainPresenter extends BasePresenterImpl<MainContract.View> implements MainContract.Presenter {
    @Override
    public void downDb(String GydwId) {
        OkGo.<String>post(Urls.YNIP + "MobileXcrzRk")
                .params("gydwid", GydwId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        MainInfo downInfo = JSON.parseObject(response.body(), MainInfo.class);
                        if (mView == null)
                            return;
                        if (downInfo.getState().equals("1")) {
                            //添加路线表
                            List<MainLxInfo> mainLxInfo = JSON.parseArray(downInfo.getLXDATA(), MainLxInfo.class);
                            if (!MyAppliCation.spUtils.getString("LX", "0").equals(mainLxInfo.size() + "")) {
                                Log.e("测试", "添加路线表");
                                LxInfoDao lxInfoDao = EntityManager.getInstance().getLxInfoDao();
                                lxInfoDao.deleteAll();
                                for (int i = 0; i < mainLxInfo.size(); i++) {
                                    LxInfo logInfo = new LxInfo();
                                    logInfo.setLXID(mainLxInfo.get(i).getLXID());
                                    logInfo.setLXMC(mainLxInfo.get(i).getLXMC());
                                    lxInfoDao.insert(logInfo);
                                }
                                MyAppliCation.spUtils.put("LX", mainLxInfo.size() + "");
                            }
                            //添加调查类型表
                            List<MainDclxInfo> mainDclxInfo = JSON.parseArray(downInfo.getDCLX(), MainDclxInfo.class);
                            if (!MyAppliCation.spUtils.getString("DCLX", "0").equals(mainLxInfo.size() + "")) {
                                Log.e("测试", "添加调查类型表");
                                DcLxInfoDao dcLxInfoDao = EntityManager.getInstance().getDcLxInfoDao();
                                dcLxInfoDao.deleteAll();
                                for (int i = 0; i < mainDclxInfo.size(); i++) {
                                    DcLxInfo dcLxInfo = new DcLxInfo();
                                    dcLxInfo.setTEXT(mainDclxInfo.get(i).getTEXT());
                                    dcLxInfo.setVALUE(mainDclxInfo.get(i).getVALUE());
                                    dcLxInfoDao.insert(dcLxInfo);
                                }
                                MyAppliCation.spUtils.put("DCLX", mainLxInfo.size() + "");
                            }
                            //添加巡查性质表
                            List<MainDclxInfo> mainXcLxInfo = JSON.parseArray(downInfo.getXCXZTREE(), MainDclxInfo.class);
                            if (!MyAppliCation.spUtils.getString("XCXZ", "0").equals(mainLxInfo.size() + "")) {
                                Log.e("测试", "添加巡查性质表");
                                XcXxInfoDao xcXxInfoDao = EntityManager.getInstance().getXcXxInfoDao();
                                xcXxInfoDao.deleteAll();
                                for (int i = 0; i < mainXcLxInfo.size(); i++) {
                                    XcXxInfo xcXxInfo = new XcXxInfo();
                                    xcXxInfo.setTEXT(mainXcLxInfo.get(i).getTEXT());
                                    xcXxInfo.setVALUE(mainXcLxInfo.get(i).getVALUE());
                                    xcXxInfoDao.insert(xcXxInfo);
                                }
                                MyAppliCation.spUtils.put("XCXZ", mainLxInfo.size() + "");
                            }
                            //添加病害表
                            List<MainBhInfo> mainBhInfo = JSON.parseArray(downInfo.getBHLXDATAGL(), MainBhInfo.class);
                            if (!MyAppliCation.spUtils.getString("BH", "0").equals(mainLxInfo.size() + "")) {
                                Log.e("测试", "添加病害表");
                                BhInfoDao bhInfoDao = EntityManager.getInstance().getBhInfoDao();
                                BhLxInfoDao bhLxInfoDao = EntityManager.getInstance().getBhLxInfoDao();
                                bhInfoDao.deleteAll();
                                bhLxInfoDao.deleteAll();
                                for (int i = 0; i < mainBhInfo.size(); i++) {
                                    BhInfo bhInfo = new BhInfo();
                                    bhInfo.setCATEGORYCODE(mainBhInfo.get(i).getCATEGORYCODE());
                                    bhInfoDao.insert(bhInfo);
                                    for (int j = 0; j < mainBhInfo.get(i).getBHLX().size(); j++) {
                                        Log.e("测试",mainBhInfo.get(i).getBHLX().get(j).getBHLXID());
                                        BhLxInfo bhLxInfo = new BhLxInfo();
                                        bhLxInfo.setBHLX(mainBhInfo.get(i).getBHLX().get(j).getBHLX());
                                        bhLxInfo.setBHLXID(mainBhInfo.get(i).getBHLX().get(j).getBHLXID());
                                        bhLxInfo.setJLDW(mainBhInfo.get(i).getBHLX().get(j).getJLDW());
                                        bhLxInfo.setSFJL(mainBhInfo.get(i).getBHLX().get(j).getSFJL());
                                        bhLxInfo.setParentId(bhInfo.getId());
                                        bhLxInfoDao.insert(bhLxInfo);
                                    }
                                }
                                MyAppliCation.spUtils.put("BH", mainLxInfo.size() + "");
                            }
                            mView.getData();
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        mView.onRequestError(response.body());
                    }
                });
    }
}
